package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.demo.common.pojo.protocol.JsonResult;
import cn.tedu.micro.demo.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.order.feign.DeviceClient;
import cn.tedu.micro.demo.order.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private DeviceClient deviceClient;

    @Override
    public String addOrder(Integer gunId, Integer userId) {
        log.debug("创建订单，枪支ID：{}，订单ID：{}",gunId,userId);
        //判断枪状态
        JsonResult<GunInfoVO> result = deviceClient.detail(gunId.longValue());
        GunInfoVO gunInfoVO = result.getData();
        if(gunInfoVO != null){
            log.info("查询结果：{}",gunInfoVO);
            Integer status = gunInfoVO.getStatus();
            if(status == 1){
                //创建订单
                OrderInfoPO  orderInfoPO = new OrderInfoPO();
                String bullId = UUID.randomUUID().toString();
                orderInfoPO.setBillId(bullId);
                orderInfoPO.setGunId(gunId.longValue());
                orderInfoPO.setUserId(userId.longValue());
                orderInfoPO.setCreateTime(new Date());
                orderRepository.save(orderInfoPO);
                return bullId;
            }
        }
        return null;
    }
}
